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R0cHM6XC9cL3Njb250ZW50LWhrZzQtMS54eC5mYmNkbi5uZXRcL3ZcL3QxLjMwNDk3LTFcLzg0NjI4MjczXzE3NjE1OTgzMDI3Nzg1Nl85NzI2OTMzNjM5MjI4MjkzMTJfbi5qcGc_c3RwPWMyOS4wLjEwMC4xMDBhX2RzdC1qcGdfcDEwMHgxMDAmX25jX2NhdD0xJmNjYj0xLTcmX25jX3NpZD01ZjIwNDgmX25jX29oYz0zWlpLazEwaExvRUFiNTZtNjd2Jl9uY19odD1zY29udGVudC1oa2c0LTEueHgmb2g9MDBfQWZDbXRseUxRdHNWeDFwZ01VTE1IMVdGQmgzZFl0UmJPVUNuY2VRRkhiT2F0USZvZT02NjQ5RkJEOSJ9.R43KEwQyUnXtjimCTrDHlUPQk92LY-XsPx97KGSXcL3GCwGpwm0YVS3s3y7wBhU_oSchJ09EwNDcAWRr6GCIMSljOdFTaQwoNG6N6mnTlhLQ49ul6nxlgJpYVxDr7N0eVaK0r-1CRqGVMuidAIeIhPBMzUVuOKUA0qwuAOeFdvHLUWbuO5-iTd15HYI0yw8V9Jw3E711RpeFp7ZTSfPa7YEKGQjCSmXtqqxiWA5IcH-Eat_4S38W1Ct6gDGrkYcUYraLcKTG1J1-39tkvS9m4Ej-ndLLhzzdhi-ZEqPjdOS3gjpjaAw_zY873I9tJEDNDtNqJ_E3E4vH9s6gVltfzQ"

func jwtDecodeSegment(seg string) ([]byte, error) {
	if l := len(seg) % 4; l > 0 {
		seg += strings.Repeat("=", 4-l)
	}

	return base64.URLEncoding.DecodeString(seg)
}

func main() {
	tokens := strings.Split(jwtPayload, ".")
	if len(tokens) != 3 {
		panic("xxxxxx")
	}
	fmt.Println(tokens[0])
	//undecodeStr, err := base64.RawStdEncoding.DecodeString(tokens[0])
	undecodeStr, err := jwtDecodeSegment(tokens[0])
	fmt.Println(string(undecodeStr), err)
	fmt.Println(tokens[1])
	//undecodeStr, err = base64.RawStdEncoding.DecodeString(tokens[1])
	undecodeStr, err = jwtDecodeSegment(tokens[1])
	fmt.Println(string(undecodeStr), err)
	fmt.Println("-------------------------------------")
	undecodeStr, err = base64.RawURLEncoding.DecodeString("-rJ0HvlxiqOcwfpP6LsAYo0aaGNmohEBFr1JuWCGVvnPb3Z5Akd5w_bxQMRlOMot15IyrhWFonWCFr9H02f9E9GOEaroAj0zxQnCXcuGWb1BFN6RfoGNFpee1MqSDV3ikSIsSI3JL-z_1uBtDsQ1AtbYMKsB572v64bapW4WjDjekz0pQ-ePizVWm9mNNQxkA_fh3p1hW3KssXgnasWbKJODT5I6hnzd4whxj22oLE8xJCYTFeouk86teKI-nvK-LmaxoetBhnDn3QS5pN_oiIfDqjKPXGazeG2qwGAE8VPeISPvzYIstGbEh3NCzFEoB7a7APF1nLEo7Lco9aWjYQ")
	fmt.Println(string(undecodeStr), err)
	nData := new(big.Int).SetBytes(undecodeStr)
	undecodeStr, err = base64.RawURLEncoding.DecodeString("AQAB")
	fmt.Println(string(undecodeStr), err)
	eData := new(big.Int).SetBytes(undecodeStr)

	var pubKey rsa.PublicKey
	pubKey.N = nData
	pubKey.E = int(eData.Uint64())
	rawData := []byte("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImVjMTFkNTAzNDFjMDhlODI4OTk2NTBlNmFmY2M2NjY4ZjJhMGE0MjAifQ.eyJpc3MiOiJodHRwczpcL1wvd3d3LmZhY2Vib29rLmNvbSIsImF1ZCI6IjI4NTAwODIyNjg1ODM5ODIiLCJzdWIiOiI0MDMzMzIzNDk2NzYxNzA2IiwiaWF0IjoxNzEzMzQwNDg3LCJleHAiOjE3MTMzNDQwODcsImp0aSI6Im1HUHAuZTA3MGE1YmYzNjJjZWQ5ZTBjNDMxM2NhZjIxZmI4ZmUxNDliNWZhNzhlZDliZDc3YTA4Y2I5MzNkMjlhODAzMSIsIm5vbmNlIjoiOUUwQjM3OTctQjczMi00ODFDLUIwQkEtMTQwQTkzQzA2RDhGIiwiYXRfaGFzaCI6ImJQWVM5bFM0VTktbzVpb3FtYWVzbnciLCJlbWFpbCI6ImxpdGUzXHUwMDQwcXEuY29tIiwiZ2l2ZW5fbmFtZSI6IlpoZXl1IiwiZmFtaWx5X25hbWUiOiJaaGFuZyIsIm5hbWUiOiJaaGV5dSBaaGFuZyIsInBpY3R1cmUiOiJodHRwczpcL1wvc2NvbnRlbnQtaGtnNC0xLnh4LmZiY2RuLm5ldFwvdlwvdDEuMzA0OTctMVwvODQ2MjgyNzNfMTc2MTU5ODMwMjc3ODU2Xzk3MjY5MzM2MzkyMjgyOTMxMl9uLmpwZz9zdHA9YzI5LjAuMTAwLjEwMGFfY3AwX2RzdC1qcGdfZTE1X3AxMDB4MTAwX3E2NSZfbmNfY2F0PTEmY2NiPTEtNyZfbmNfc2lkPTVmMjA0OCZlZmc9ZXlKcElqb2lkQ0o5Jl9uY19vaGM9SmdWYUN4dVBTQUFBYjV4OC11dCZ0bj1KdEFyR1g1UTM1NTlSZGJ3Jl9uY19odD1zY29udGVudC1oa2c0LTEueHgmb2g9MDBfQWZBbm56V1ZxQzc2OGZVNTBTaHo4Ni02eWRlWnlQQTdqd3hYNF9mRGoxSzJ3dyZvZT02NjQ2RTg1OSJ9")

	hasher := crypto.SHA256.New()
	hasher.Write(rawData)
	//digest := sha256.Sum256(rawData)
	digest := hasher.Sum(nil)
	fmt.Println("-------------------------------------222")
	undecodeStr, err = jwtDecodeSegment(tokens[2])
	fmt.Println("-------------------------------------")
	fmt.Println(string(undecodeStr), err)
	fmt.Println("-------------------------------------")
	err = rsa.VerifyPKCS1v15(&pubKey, crypto.SHA256, digest, undecodeStr)
	fmt.Println("--------------", err)

	pubKeyStr := "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImVjMTFkNTAzNDFjMDhlODI4OTk2NTBlNmFmY2M2NjY4ZjJhMGE0MjAifQ.eyJpc3MiOiJodHRwczpcL1wvd3d3LmZhY2Vib29rLmNvbSIsImF1ZCI6IjI4NTAwODIyNjg1ODM5ODIi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key: error:%v", err)
		return
	}

	rsaPubKey := pubKey2.(*rsa.PublicKey)
	if nil == rsaPubKey {
		panic(fmt.Errorf("parser pubkey error"))
	}

}
